package com.delianna.bwt.models;

import java.util.ArrayList;
import java.util.List;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * @author Delio Castillo (jangeador@gmail.com)
 *
 */
public class Port implements Parcelable {

	public String port_number;
	public String border;
	public String port_name;
	public String crossing_name;
	public String hours;
	private String date;
	public String port_status;
	private Lanes commercialLanes;
	private Lanes passengerLanes;
	private Lanes pedestrianLanes;
	public String construction;

	public Port() {
		commercialLanes = new Lanes();
		passengerLanes = new Lanes();
		pedestrianLanes = new Lanes();
	}

	public Port(Parcel in) {
		readFromParcel(in);
	}

	public String getTitle() {
		StringBuilder sb = new StringBuilder();
		sb.append(port_name);
		if (crossing_name != null) {
			sb.append(" - " + crossing_name);
		}
		return sb.toString();
	}

	public String getUpdateTime() {
		// TODO: Fix broken fall back
		/*
		 * String s; s = passengerLanes.getStandardLanes().getUpdateTime();
		 * //Fall back on commercial if (s.equals(null)) s =
		 * commercialLanes.getStandardLanes().getUpdateTime();
		 */
		return passengerLanes.getStandardLanes().getUpdateTime();
	}

	private void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		commercialLanes = in.readParcelable(Lanes.class.getClassLoader());
		passengerLanes = in.readParcelable(Lanes.class.getClassLoader());
		pedestrianLanes = in.readParcelable(Lanes.class.getClassLoader());

		port_number = in.readString();
		border = in.readString();
		port_name = in.readString();
		crossing_name = in.readString();
		hours = in.readString();
		date = in.readString();
		construction = in.readString();
	}

	public void setCommercialLanes(Lanes value) {
		this.commercialLanes = value;
	}

	public Lanes getCommercialLanes() {
		return commercialLanes;
	}

	public void setPassengerLanes(Lanes value) {
		this.passengerLanes = value;
	}

	public Lanes getPassengerLanes() {
		return passengerLanes;
	}

	public void setPedestrianLanes(Lanes value) {
		this.pedestrianLanes = value;
	}

	public Lanes getPedestrianLanes() {
		return pedestrianLanes;
	}
	
	public String getDate(){
		return date;
	}
	
	public void setDate(String value){
		date = value;
	}

	public List<Lane> getAllLanes() {
		ArrayList<Lane> allLanes = new ArrayList<Lane>();

		if (isValid(this.passengerLanes.getStandardLanes()))
			allLanes.add(this.passengerLanes.getStandardLanes());

		if (isValid(this.passengerLanes.getExpressLanes()))
			allLanes.add(this.passengerLanes.getExpressLanes());

		if (isValid(this.pedestrianLanes.getStandardLanes()))
			allLanes.add(this.pedestrianLanes.getStandardLanes());

		if (isValid(this.commercialLanes.getStandardLanes()))
			allLanes.add(this.commercialLanes.getStandardLanes());

		if (isValid(this.commercialLanes.getExpressLanes()))
			allLanes.add(this.commercialLanes.getExpressLanes());

		return allLanes;
	}

	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		String NEW_LINE = System.getProperty("line.separator");

		result.append(this.getClass().getName() + " Object {" + NEW_LINE);
		result.append(" port_number: " + port_number + NEW_LINE);
		result.append(" border: " + border + NEW_LINE);
		result.append(" port_name: " + port_name + NEW_LINE);
		result.append(" crossing_name: " + crossing_name + NEW_LINE);
		result.append(" hours: " + hours + NEW_LINE);
		result.append(" date: " + date + NEW_LINE);
		result.append(" port_status: " + port_status + NEW_LINE);
		result.append("}");

		return result.toString();
	}

	private boolean isValid(Lane l) {
		if (l.getSubtitle().trim().equals("N/A"))
			return false;
		else
			return true;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
		// TODO Auto-generated method stub

		out.writeParcelable(commercialLanes, flags);
		out.writeParcelable(passengerLanes, flags);
		out.writeParcelable(pedestrianLanes, flags);

		out.writeString(port_number);
		out.writeString(border);
		out.writeString(port_name);
		out.writeString(crossing_name);
		out.writeString(hours);
		out.writeString(date);
		out.writeString(construction);
	}

	/**
	 * 
	 * This field is needed for Android to be able to create new objects,
	 * individually or as arrays.
	 * 
	 * This also means that you can use use the default constructor to create
	 * the object and use another method to hyrdate it as necessary.
	 * 
	 * I just find it easier to use the constructor. It makes sense for the way
	 * my brain thinks ;-)
	 * 
	 */
	public static final Parcelable.Creator<Port> CREATOR = new Parcelable.Creator<Port>() {
		public Port createFromParcel(Parcel in) {
			return new Port(in);
		}

		public Port[] newArray(int size) {
			return new Port[size];
		}
	};
}
